from sqlalchemy import (
    Column, Integer, String, BigInteger, Boolean, DateTime, JSON, ForeignKey, Text
)
from sqlalchemy.dialects.postgresql import JSONB

from models import db


class MatCategoryAttr(db.Model):
    __tablename__ = 'rd_gdm_mat_category_attr'

    id = Column(BigInteger, primary_key=True, comment='数据主键id')
    master_id = Column(BigInteger, nullable=False, comment='master表id')
    sort = Column(Integer, comment='排序号')
    is_required = Column(String(2), comment='是否必填')
    is_syn_desc = Column(String(2), comment='是否参与物料描述合成')
    is_syn_long_desc = Column(String(2), comment='是否参与物料长描述合成')
    is_assign_code_valid = Column(String(2), comment='是否参与赋码校验')
    value_range = Column(String(50), comment='值范围')
    default_value = Column(String(50), comment='默认值')
    lifecycle_code = Column(String(50), comment='生命周期状态')
    iteration = Column(String(50), comment='小版本')
    version = Column(String(50), comment='版本号')
    process_instance_id = Column(String(64), comment='流程实例id')
    process_status = Column(String(50), comment='流程状态')
    wrk_info = Column(String(50), comment='检出标识')
    tenant_id = Column(BigInteger, comment='租户号')
    remarks = Column(String(255), comment='备注')
    created_by = Column(BigInteger, comment='创建人')
    creation_date = Column(DateTime, comment='创建时间')
    last_updated_by = Column(BigInteger, comment='更新人')
    last_update_date = Column(DateTime, comment='更新时间')
    version_num = Column(BigInteger, comment='迭代版本')
    last_update_login = Column(BigInteger, comment='最近登录')
    operator_user_id = Column(BigInteger, comment='操作人id')
    delete_flag = Column(Integer, nullable=False, default=0, comment='删除标识')


class Material(db.Model):
    __tablename__ = 'rd_gdm_material'

    id = Column(BigInteger, primary_key=True, comment='数据主键id')
    master_id = Column(BigInteger, comment='master表id')
    zh_desc = Column(String(50), nullable=False, comment='物料中文描述')
    en_desc = Column(String(50), comment='物料英文描述')
    long_desc = Column(String(255), comment='物料长描述')
    lifecycle_code = Column(String(50), comment='生命周期状态')
    restricted_code = Column(String(50), comment='受限状态')
    restricted_time = Column(DateTime, comment='受限状态时间（失效时间）')
    iteration = Column(String(50), comment='小版本')
    wrk_info = Column(String(2), comment='检出标识')
    version = Column(String(50), comment='版本')
    process_instance_id = Column(String(64), comment='关联流程实例id')
    process_status = Column(String(50), comment='流程状态')
    release_time = Column(DateTime, comment='发布时间')
    source_material_number = Column(String(50), comment='被认证件的原物料编码(认证件)/总成件物料编码(拆分子件)')
    supplier = Column(String(255), comment='总成件供应商(拆分子件)')
    tenant_id = Column(BigInteger, comment='租户号')
    remarks = Column(String(255), comment='备注')
    created_by = Column(BigInteger, comment='创建人')
    creation_date = Column(DateTime, comment='创建时间')
    last_updated_by = Column(BigInteger, comment='更新人')
    last_update_date = Column(DateTime, comment='更新时间')
    version_num = Column(BigInteger, comment='迭代版本')
    last_update_login = Column(BigInteger, comment='最近登录')
    operator_user_id = Column(BigInteger, comment='操作人id')
    delete_flag = Column(Integer, nullable=False, default=0, comment='删除标识')


class MatAttrData(db.Model):
    __tablename__ = 'rd_gdm_mat_attr_data'

    id = Column(BigInteger, primary_key=True, comment='数据主键id')
    material_id = Column(BigInteger, comment='物料id')
    model = Column(String(50), comment='图号/型号')
    net_weight = Column(String(50), comment='净重')
    weight_unit = Column(String(50), comment='重量单位')
    basic_unit = Column(String(50), comment='基本计量单位')
    external_material_group = Column(String(50), comment='外部物料组')
    material_group = Column(String(50), comment='物料组')
    material_classification = Column(String(50), comment='物料大类')
    design_category = Column(String(50), comment='设计分类')
    import_level = Column(String(50), comment='重要度')
    import_domestic = Column(String(50), comment='进口/国外')
    std_industry_desc = Column(String(50), comment='行业标准描述')
    cert_requirement = Column(String(50), comment='认证要求')
    design_desc = Column(String(50), comment='设计说明')
    material_type = Column(String(50), comment='物料类型')
    basic_material = Column(String(50), comment='基本物料（材质）')
    xvpm_project = Column(String(50), comment='XVPM项目')
    is_cirtical_component = Column(String(4), comment='是否关重件')
    purchase_material_number = Column(String(50), comment='采购物料号')
    design_material_type = Column(String(50), comment='设计类物料类别')
    remark = Column(String(255), comment='备注')
    global_procurement = Column(String(4), comment='全球采购优选')
    purchase_procurement = Column(String(4), comment='采购优选')
    unified_procurement = Column(String(4), comment='统型优选')
    product_dev_type = Column(String(50), comment='产品开发类别')
    config_desc = Column(String(255), comment='配置说明')
    appearance_size = Column(String(50), comment='外观尺寸（计量属性）')
    spray_area = Column(String(50), comment='喷涂面积（计量属性）')
    dimension = Column(String(50), comment='大小/量纲（计量属性）')
    volume = Column(String(50), comment='体积（计量属性）')
    volume_unit = Column(String(50), comment='体积单位（计量属性）')
    area = Column(String(50), comment='面积（计量属性）')
    area_unit = Column(String(50), comment='面积单位（计量属性）')
    is_outsourced_split = Column(String(4), comment='是否外购件拆分件（备件专有属性）')
    performance_property = Column(String(50), comment='性能属性（备件专有属性）')
    usage_property = Column(String(50), comment='使用属性（备件专有属性）')
    attr_data = Column(JSONB, comment='扩展属性信息')
    tenant_id = Column(BigInteger, comment='租户号')
    remarks = Column(String(255), comment='备注')
    created_by = Column(BigInteger, comment='创建人')
    creation_date = Column(DateTime, comment='创建时间')
    last_updated_by = Column(BigInteger, comment='更新人')
    last_update_date = Column(DateTime, comment='更新时间')
    version_num = Column(BigInteger, comment='迭代版本')
    last_update_login = Column(BigInteger, comment='最近登录')
    operator_user_id = Column(BigInteger, comment='操作人id')
    delete_flag = Column(Integer, nullable=False, default=0, comment='删除标识')


class MatAttrPool(db.Model):
    __tablename__ = 'rd_gdm_mat_attr_pool'

    id = Column(BigInteger, primary_key=True, comment='数据主键id')
    master_id = Column(BigInteger, comment='master表id')
    sort = Column(Integer, comment='排序号')
    description = Column(String(50), comment='属性描述')
    data_type = Column(String(50), comment='数据类型')
    unit = Column(String(50), comment='单位')
    input_type = Column(String(10), comment='输入类型')
    dict_type = Column(String(50), comment='字典类型')
    version = Column(String(50), comment='版本号')
    iteration = Column(String(50), comment='小版本')
    wrk_info = Column(String(50), comment='检出标识')
    lifecycle_code = Column(String(50), comment='生命周期状态')
    process_instance_id = Column(String(64), comment='关联流程实例id')
    process_status = Column(String(50), comment='流程状态')
    tenant_id = Column(BigInteger, comment='租户号')
    remarks = Column(String(255), comment='备注')
    created_by = Column(BigInteger, comment='创建人')
    creation_date = Column(DateTime, comment='创建时间')
    last_updated_by = Column(BigInteger, comment='更新人')
    last_update_date = Column(DateTime, comment='更新时间')
    version_num = Column(BigInteger, comment='迭代版本')
    last_update_login = Column(BigInteger, comment='最近登录')
    operator_user_id = Column(BigInteger, comment='操作人id')
    delete_flag = Column(Integer, nullable=False, default=0, comment='删除标识')


class MatAttrPoolMaster(db.Model):
    __tablename__ = 'rd_gdm_mat_attr_pool_master'

    id = Column(BigInteger, primary_key=True, comment='数据主键id')
    code = Column(String(50), comment='属性编码')
    name = Column(String(50), comment='属性中文名称')
    en_name = Column(String(50), comment='属性英文名称')
    status = Column(String(50), comment='状态')
    tenant_id = Column(BigInteger, comment='租户号')
    remarks = Column(String(255), comment='备注')
    created_by = Column(BigInteger, comment='创建人')
    creation_date = Column(DateTime, comment='创建时间')
    last_updated_by = Column(BigInteger, comment='更新人')
    last_update_date = Column(DateTime, comment='更新时间')
    version_num = Column(BigInteger, comment='迭代版本')
    last_update_login = Column(BigInteger, comment='最近登录')
    operator_user_id = Column(BigInteger, comment='操作人id')
    delete_flag = Column(Integer, nullable=False, default=0, comment='删除标识')



class MatCategory(db.Model):
    __tablename__ = 'rd_gdm_mat_category'

    id = Column(BigInteger, primary_key=True, comment='数据主键id')
    parent_tree_code = Column(String(50), default='0', comment='父级树节点编码')
    tree_code = Column(String(50), comment='分类树节点编码/内部唯一标识')
    path = Column(String(255), comment='完整路径')
    level = Column(String(50), comment='分类层级')
    is_leaf = Column(String(4), comment='是否分类最底层')
    name = Column(String(50), nullable=False, comment='中文名称')
    en_name = Column(String(50), comment='英文名称')
    zh_desc = Column(String(50), comment='中文描述')
    en_desc = Column(String(50), comment='英文描述')
    basis = Column(String(50), comment='分类依据')
    code = Column(String(50), comment='分类码')
    identifier = Column(String(50), comment='分类标识')
    part_attr = Column(String(50), comment='备件使用属性')
    status = Column(String(50), comment='状态')
    tenant_id = Column(BigInteger, comment='租户号')
    remarks = Column(String(255), comment='备注')
    created_by = Column(BigInteger, comment='创建人')
    creation_date = Column(DateTime, comment='创建时间')
    last_updated_by = Column(BigInteger, comment='更新人')
    last_update_date = Column(DateTime, comment='更新时间')
    version_num = Column(BigInteger, comment='迭代版本')
    last_update_login = Column(BigInteger, comment='最近登录')
    operator_user_id = Column(BigInteger, comment='操作人id')
    delete_flag = Column(Integer, nullable=False, default=0, comment='删除标识')


class MaterialMaster(db.Model):
    __tablename__ = 'rd_gdm_material_master'

    id = Column(BigInteger, primary_key=True, comment='主键id')
    category_tree_code = Column(String(50), comment='分类树节点编码')
    number = Column(String(50), comment='物料编码')
    temp_number = Column(String(12), comment='物料临时码')
    from_system = Column(String(50), comment='来源系统')
    name = Column(String(50), comment='物料名称')
    en_name = Column(String(255), comment='物料的英文名称')
    soft_type = Column(String(50), comment='业务分类')
    tenant_id = Column(BigInteger, comment='租户id')
    remarks = Column(String(255), comment='备注')
    created_by = Column(BigInteger, comment='创建人')
    creation_date = Column(DateTime, comment='创建时间')
    last_updated_by = Column(BigInteger, comment='更新人')
    last_update_date = Column(DateTime, comment='更新时间')
    version_num = Column(BigInteger, comment='迭代版本')
    last_update_login = Column(BigInteger, comment='最近登录')
    operator_user_id = Column(BigInteger, comment='操作人id')
    delete_flag = Column(Integer, comment='删除标识')



class MatCategoryAttrMaster(db.Model):
    __tablename__ = 'rd_gdm_mat_category_attr_master'

    id = Column(BigInteger, primary_key=True, comment='主键id')
    category_tree_code = Column(String(50), comment='分类树节点编码')
    attr_pool_master_id = Column(BigInteger, ForeignKey('rd_gdm_mat_attr_pool_master.id'), nullable=False,
                                 comment='属性池master表id')
    tenant_id = Column(BigInteger, comment='租户id')
    remarks = Column(String(255), comment='备注')
    created_by = Column(BigInteger, comment='创建人')
    creation_date = Column(DateTime, comment='创建时间')
    last_updated_by = Column(BigInteger, comment='更新人')
    last_update_date = Column(DateTime, comment='更新时间')
    version_num = Column(BigInteger, comment='迭代版本')
    last_update_login = Column(BigInteger, comment='最近登录')
    operator_user_id = Column(BigInteger, comment='操作人id')
    delete_flag = Column(Integer, comment='删除标识')


class RdGdmMatFactory(db.Model):
    __tablename__ = "rd_gdm_mat_factory"
    __table_args__ = {"comment": "工厂基本信息表"}

    id = Column(Integer, primary_key=True, comment="主键id")
    name = Column(String(50), comment="工厂名称")
    code = Column(String(50), comment="工厂编码")
    owning_dept = Column(String(50), comment="所属单位")


class ReMaterialRule(db.Model):
    __tablename__ = "re_material_rule"
    __bind_key__ = "rule_db"

    rule_code = Column(String, comment="规则CODE")
    uid = Column(String, primary_key=True, comment="")
    creation_time = Column(String, comment="创建时间")
    creator_id = Column(String, comment="创建人")
    creator_name = Column(String, comment="创建人名称")
    update_time = Column(String, comment="更新时间")
    updater_id = Column(String, comment="更新人ID")
    updater_name = Column(String, comment="更新人名称")
    remark = Column(String, comment="备注")
    priority = Column(Integer, comment="优先级")
    active = Column(Boolean, comment="是否启用 ture启用")
    attr_type = Column(String, comment="属性类别 single为单一属性  multi为多个级联属性")
    conditions_group = Column(JSON, comment="存储json形式条件")
    results = Column(JSON, comment="存储json形式结果")
    attr_classify = Column(String, comment="属性类型分类节点")
    del_flag = Column(Boolean, default=False, comment="删除标志")
    description = Column(String, comment="描述")
    error_code = Column(String, comment="错误码")
    error_msg = Column(String, comment="错误提示")



class ReRuleInfo(db.Model):
    __tablename__ = "re_rule_info"
    __bind_key__ = "rule_db"

    uid = Column(String, primary_key=True, comment="主键")
    del_flag = Column(Boolean, default=False, comment="删除标志")
    logical_operator = Column(String, comment="连接条件 and or")
    operator = Column(String, comment="运算符")
    field = Column(String, comment="字段")
    value = Column(String, comment="值")
    parent_id = Column(String, comment="父ID")
    material_rule_id = Column(String, ForeignKey("re_material_rule.uid"),
                              comment="规则列表关联ID")
    type = Column(String, comment="规则类型 条件规则 conditionsGroup  结果规则 results")
    level = Column(Integer, comment="层级")


class SimilarTask(db.Model):
    __tablename__ = "similar_task"
    __bind_key__ = "rule_db"
    id = Column(Integer, primary_key=True)
    task_id = Column(String)
    status = Column(Integer)
    request = Column(String, comment="请求信息")
    match = Column(String, comment="匹配结果")
    response = Column(String, comment="返回信息")
    times = Column(Integer)
    create_time = Column(DateTime)
